{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sharing pipelines and models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Share your pipeline or models and schedulers on the Hub with the [PushToHubMixin](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.utils.PushToHubMixin) class. This class:\n",
    "\n",
    "1. creates a repository on the Hub\n",
    "2. saves your model, scheduler, or pipeline files so they can be reloaded later\n",
    "3. uploads folder containing these files to the Hub\n",
    "\n",
    "This guide will show you how to upload your files to the Hub with the [PushToHubMixin](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.utils.PushToHubMixin) class.\n",
    "\n",
    "Log in to your Hugging Face account with your access [token](https://huggingface.co/settings/tokens).\n",
    "\n",
    "<hfoptions id=\"login\">\n",
    "<hfoption id=\"notebook\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from huggingface_hub import notebook_login\n",
    "\n",
    "notebook_login()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</hfoption>\n",
    "<hfoption id=\"hf CLI\">\n",
    "\n",
    "```bash\n",
    "hf auth login\n",
    "```\n",
    "\n",
    "</hfoption>\n",
    "</hfoptions>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To push a model to the Hub, call [push_to_hub()](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) and specify the repository id of the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from diffusers import ControlNetModel\n",
    "\n",
    "controlnet = ControlNetModel(\n",
    "    block_out_channels=(32, 64),\n",
    "    layers_per_block=2,\n",
    "    in_channels=4,\n",
    "    down_block_types=(\"DownBlock2D\", \"CrossAttnDownBlock2D\"),\n",
    "    cross_attention_dim=32,\n",
    "    conditioning_embedding_out_channels=(16, 32),\n",
    ")\n",
    "controlnet.push_to_hub(\"my-controlnet-model\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The [push_to_hub()](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) method saves the model's `config.json` file and the weights are automatically saved as safetensors files.\n",
    "\n",
    "Load the model again with [from_pretrained()](https://huggingface.co/docs/diffusers/main/en/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ControlNetModel.from_pretrained(\"your-namespace/my-controlnet-model\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scheduler"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To push a scheduler to the Hub, call [push_to_hub()](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) and specify the repository id of the scheduler."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from diffusers import DDIMScheduler\n",
    "\n",
    "scheduler = DDIMScheduler(\n",
    "    beta_start=0.00085,\n",
    "    beta_end=0.012,\n",
    "    beta_schedule=\"scaled_linear\",\n",
    "    clip_sample=False,\n",
    "    set_alpha_to_one=False,\n",
    ")\n",
    "scheduler.push_to_hub(\"my-controlnet-scheduler\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The [push_to_hub()](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) function saves the scheduler's `scheduler_config.json` file to the specified repository.\n",
    "\n",
    "Load the scheduler again with [from_pretrained()](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.SchedulerMixin.from_pretrained)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scheduler = DDIMScheduler.from_pretrained(\"your-namepsace/my-controlnet-scheduler\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pipeline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To push a pipeline to the Hub, initialize the pipeline components with your desired parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from diffusers import (\n",
    "    UNet2DConditionModel,\n",
    "    AutoencoderKL,\n",
    "    DDIMScheduler,\n",
    "    StableDiffusionPipeline,\n",
    ")\n",
    "from transformers import CLIPTextModel, CLIPTextConfig, CLIPTokenizer\n",
    "\n",
    "unet = UNet2DConditionModel(\n",
    "    block_out_channels=(32, 64),\n",
    "    layers_per_block=2,\n",
    "    sample_size=32,\n",
    "    in_channels=4,\n",
    "    out_channels=4,\n",
    "    down_block_types=(\"DownBlock2D\", \"CrossAttnDownBlock2D\"),\n",
    "    up_block_types=(\"CrossAttnUpBlock2D\", \"UpBlock2D\"),\n",
    "    cross_attention_dim=32,\n",
    ")\n",
    "\n",
    "scheduler = DDIMScheduler(\n",
    "    beta_start=0.00085,\n",
    "    beta_end=0.012,\n",
    "    beta_schedule=\"scaled_linear\",\n",
    "    clip_sample=False,\n",
    "    set_alpha_to_one=False,\n",
    ")\n",
    "\n",
    "vae = AutoencoderKL(\n",
    "    block_out_channels=[32, 64],\n",
    "    in_channels=3,\n",
    "    out_channels=3,\n",
    "    down_block_types=[\"DownEncoderBlock2D\", \"DownEncoderBlock2D\"],\n",
    "    up_block_types=[\"UpDecoderBlock2D\", \"UpDecoderBlock2D\"],\n",
    "    latent_channels=4,\n",
    ")\n",
    "\n",
    "text_encoder_config = CLIPTextConfig(\n",
    "    bos_token_id=0,\n",
    "    eos_token_id=2,\n",
    "    hidden_size=32,\n",
    "    intermediate_size=37,\n",
    "    layer_norm_eps=1e-05,\n",
    "    num_attention_heads=4,\n",
    "    num_hidden_layers=5,\n",
    "    pad_token_id=1,\n",
    "    vocab_size=1000,\n",
    ")\n",
    "text_encoder = CLIPTextModel(text_encoder_config)\n",
    "tokenizer = CLIPTokenizer.from_pretrained(\"hf-internal-testing/tiny-random-clip\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pass all components to the pipeline and call [push_to_hub()](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "components = {\n",
    "    \"unet\": unet,\n",
    "    \"scheduler\": scheduler,\n",
    "    \"vae\": vae,\n",
    "    \"text_encoder\": text_encoder,\n",
    "    \"tokenizer\": tokenizer,\n",
    "    \"safety_checker\": None,\n",
    "    \"feature_extractor\": None,\n",
    "}\n",
    "\n",
    "pipeline = StableDiffusionPipeline(**components)\n",
    "pipeline.push_to_hub(\"my-pipeline\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The [push_to_hub()](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) method saves each component to a subfolder in the repository. Load the pipeline again with [from_pretrained()](https://huggingface.co/docs/diffusers/main/en/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipeline = StableDiffusionPipeline.from_pretrained(\"your-namespace/my-pipeline\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Privacy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set `private=True` in [push_to_hub()](https://huggingface.co/docs/diffusers/main/en/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) to keep a model, scheduler, or pipeline files private."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "controlnet.push_to_hub(\"my-controlnet-model-private\", private=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Private repositories are only visible to you. Other users won't be able to clone the repository and it won't appear in search results. Even if a user has the URL to your private repository, they'll receive a `404 - Sorry, we can't find the page you are looking for`. You must be [logged in](https://huggingface.co/docs/huggingface_hub/quick-start#login) to load a model from a private repository."
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 4
}
